package com.biostime.dingdingbox.modules.db.service;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.dingdingbox.common.api.ObjResponse;
import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.service.CrudService;
import com.biostime.dingdingbox.modules.db.dao.MemberBoxDao;
import com.biostime.dingdingbox.modules.db.dto.MemberDto;
import com.biostime.dingdingbox.modules.db.entity.BoxCode;
import com.biostime.dingdingbox.modules.db.entity.Member;
import com.biostime.dingdingbox.modules.db.entity.MemberBox;
import com.biostime.dingdingbox.modules.db.entity.PlatformMember;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 激活记录表Service
 * @author chenweicheng
 * @version 2016-06-28
 */
@Service
@Transactional
public class MemberBoxService extends CrudService<MemberBoxDao, MemberBox> {
	
	@Autowired 
	private MemberService memberService;
	
	@Autowired
	private PlatformMemberService platformMemberService;
	
	public List<MemberBox> apiGetMBByMemberId(String userId , String boxId){
		return dao.apiGetMBByMemberId(userId , boxId);
	}
	
	public MemberBox apiGetByCodeAndMem(String userId , String code){
		return dao.apiGetByCodeAndMem(userId, code);
	}
	
	public MemberBox apiGetByForeign(String memberId,String code){
		return dao.apiGetByForeign(memberId , code);
	}
		
	public int apiInsert(String userId ,BoxCode boxCode) throws JsonParseException, JsonMappingException, IOException{
		
		//获取会员信息
		String str = new String(HttpRequestService.sendGet(Global.getConfig("ParentPowerServer")+
				"/qdingding-webapp/a/qd/api/out/getMemberInfo", "userId="+userId));		
		ObjectMapper mapper = new ObjectMapper();
		ObjResponse<MemberDto> entity = mapper.readValue(str,new TypeReference<ObjResponse<MemberDto>>(){});							
		MemberDto mem = entity.getObj();
		
		if(mem != null){
			
			//如果不存在该用户记录
			if(memberService.apiGetMemByForeign(userId) == null){
				
				//新增用户记录
				Member member = new Member();
				member.setAddress(mem.getAddress());
				member.setMobile(mem.getMobile());
				member.setPassword(mem.getMobile());
				member.setName(mem.getNickname()); 
				member.setLoginFlag("0"); 
				memberService.save(member);   
				
				//新增平台会员记录
				PlatformMember platformMember = new PlatformMember();
				platformMember.setForeignId(String.valueOf(mem.getId()));
				platformMember.setNickname(mem.getNickname());
				platformMember.setMemberId(Integer.parseInt(member.getId()));
				platformMember.setPlatform("parentPower"); 
				platformMemberService.save(platformMember); 		
				
			}
									
			//新增激活记录
			MemberBox memberBox = new MemberBox();
			memberBox.setBoxCodeId(Integer.parseInt(boxCode.getId()));
			memberBox.setBoxId(boxCode.getBoxId());
			memberBox.setCode(boxCode.getCode());
			memberBox.setIsActivated(1);			
			memberBox.setMemberId(Integer.parseInt(memberService.apiGetUserId(userId)));  
			save(memberBox);			
			
		}else{
			return 1;
		}
		
		return 0;
	}
}
